﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace SoftballStats_BL
{
    public partial class Game
    {
#region Other Properties
        private Guid _ID;
        private Guid _TeamID;
        private short _TeamScore;
        private short _OpponentScore;
        private bool _IsWin;
        private bool _IsHome;
        private DateTime _Date;
        private string _Location;
        private string _OpponentName;

        public Guid ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        public Guid TeamID
        {
            get
            {
                return _TeamID;
            }
            set
            {
                _TeamID = value;
            }
        }

        public short TeamScore
        {
            get
            {
                return _TeamScore;
            }
            set
            {
                _TeamScore = value;
            }
        }

        public short OpponentScore
        {
            get
            {
                return _OpponentScore;
            }
            set
            {
                _OpponentScore = value;
            }
        }

        public bool IsWin
        {
            get
            {
                return _IsWin;
            }
            set
            {
                _IsWin = value;
            }
        }

        public bool IsHome
        {
            get
            {
                return _IsHome;
            }
            set
            {
                _IsHome = value;
            }
        }

        public DateTime Date
        {
            get
            {
                return _Date;
            }
            set
            {
                _Date = value;
            }
        }

        public string Location
        {
            get
            {
                return _Location;
            }
            set
            {
                _Location = value;
            }
        }

        public string OpponentName
        {
            get
            {
                return _OpponentName;
            }
            set
            {
                _OpponentName = value;
            }
        }
#endregion

#region Public Methods
        public static List<Game> GetGamesByTeamID(Guid teamID)
        {
            List<SoftballStats_BL.Game> lstGames = new List<SoftballStats_BL.Game>();

            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetGamesByTeamID";
                    cmd.Parameters.AddWithValue("@TeamID", teamID);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            Game thisGame = new Game();
                            thisGame.ID = Guid.Parse(dr["ID"].ToString());
                            thisGame.TeamScore = short.Parse(dr["TeamScore"].ToString());
                            thisGame.OpponentScore = short.Parse(dr["OpponentScore"].ToString());
                            thisGame.IsWin = bool.Parse(dr["IsWin"].ToString());
                            thisGame.IsHome = bool.Parse(dr["IsHome"].ToString());
                            thisGame.Date = DateTime.Parse(dr["Date"].ToString());
                            thisGame.Location = dr["Location"].ToString();
                            thisGame.OpponentName = dr["OpponentName"].ToString();
                            lstGames.Add(thisGame);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get Games", ex);
            }

            return lstGames;
        }

        public static Game GetGameByID(Guid gameID)
        {
            Game thisGame = new Game();
            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetGameByID";
                    cmd.Parameters.AddWithValue("@ID", gameID);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            thisGame.ID = Guid.Parse(dr["ID"].ToString());
                            thisGame.TeamID = Guid.Parse(dr["TeamID"].ToString());
                            thisGame.TeamScore = short.Parse(dr["TeamScore"].ToString());
                            thisGame.OpponentScore = short.Parse(dr["OpponentScore"].ToString());
                            thisGame.IsWin = bool.Parse(dr["IsWin"].ToString());
                            thisGame.IsHome = bool.Parse(dr["IsHome"].ToString());
                            thisGame.Date = DateTime.Parse(dr["Date"].ToString());
                            thisGame.Location = dr["Location"].ToString();
                            thisGame.OpponentName = dr["OpponentName"].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get game", ex);
            }
            return thisGame;
        }
#endregion
    }
}
